package utils

import (
	"crypto/md5"
	"encoding/hex"
	"user-center/src/constant"
	"user-center/src/model"
)

// EncryptMd5 md5 的加密算法
func EncryptMd5(userPassword string) string {
	h := md5.New()
	h.Write([]byte(constant.SALT + userPassword))
	return hex.EncodeToString(h.Sum(nil))
}

// GetSafetyUser 用户信息脱敏
func GetSafetyUser(user *model.User) model.User {
	safetyUser := model.User{}
	safetyUser.Id = user.Id
	safetyUser.Username = user.Username
	safetyUser.UserAccount = user.UserAccount
	safetyUser.AvatarUrl = user.AvatarUrl
	safetyUser.Gender = user.Gender
	safetyUser.Phone = user.Phone
	safetyUser.Email = user.Email
	safetyUser.UserStatus = user.UserStatus
	safetyUser.UserRole = user.UserRole
	safetyUser.CreateTime = user.CreateTime
	safetyUser.Tags = user.Tags
	return safetyUser
}

// Unique  函数用于去重切片中的元素
func Unique(intSlice []int64) []int64 {
	keys := make(map[int64]bool)
	var list []int64
	for _, entry := range intSlice {
		if _, value := keys[entry]; !value {
			keys[entry] = true
			list = append(list, entry)
		}
	}
	return list
}
